# -*- coding: utf-8 -*-

from lxml import etree

class Item:

    stats = {'vi':['vitalité',0,0.25],
             'cha':['chance',0,1.0],
             'age':['agilité',0,1.0],
             'ine':['intelligence',0,1.0],
             'fo':['force',0,1.0],
             'sa':['sagesse',0,3.0],
             'pui':['puissance',0,2.0],
             'pa':['PA',0,100.0],
             'pm':['PM',0,90.0],
             'po':['portée',0,51.0],
             'do':['dommage',0,20.0],
             'doeau':['dommage eau',0,5.0],
             'doair':['dommage air',0,5.0],
             'dofeu':['dommage feu',0,5.0],
             'doneu':['dommage neutre',0,5.0],
             'doter':['dommage terre',0,5.0],
             'docri':['dommage critique',0,5.0],
             'cc':['coup critique',0,30.0],
             'invo':['créature invoquable',0,30.0],
             'ini':['initiative',0,0.1],
             'pod':['pod',0,0.25],
             'reeau':['résistance fixe eau',0,2.0],
             'reair':['résistance fixe air',0,2.0],
             'refeu':['résistance fixe feu',0,2.0],
             'reneu':['résistance fixe neutre',0,2.0],
             'reter':['résistance fixe terre',0,2.0],
             'recri':['résistance fixe critique',0,2.0],
             'repereau':['résistance % eau',0,6.0],
             'reperair':['résistance % air',0,6.0],
             'reperfeu':['résistance % feu',0,6.0],
             'reperneu':['résistance % neutre',0,6.0],
             'reperter':['résistance % terre',0,6.0],
             'retpa':['retrait PA',0,7.0],
             'retpm':['retrait PM',0,7.0],
             'respa':['résistance PA',0,7.0],
             'respm':['réssitance PM',0,7.0],
             'fui':['fuite',0,4.0],
             'tac':['tacle',0,4.0]
             }
    
    categories = ['cac','coiffe','cape','bottes','ceinture','anneau','amulette','familier','monture','dofus','trophée']
    category = None
    level = 0
    name = None

    def __init__(self,name=None):
        if name!=None: self.load()
        else:
            while(self.ask()): pass

    def ask(self):
        cmd = raw_input('item>')
        cmd = cmd.split(' ')
        if cmd[0] in self.stats.keys() and len(cmd)>1:
            try:
                self.stats[cmd[0]][1]=int(cmd[1])
            except ValueError:
                print 'Valeur invalide'
        elif cmd[0]=='end': return False
        elif cmd[0]=='name' and len(cmd)>1:
            self.name = " ".join(cmd[1:])
        elif cmd[0]=='type' and len(cmd)>1:
            if cmd[1] in self.categories:
                self.category = self.categories.index(cmd[1])
            else:
                print 'Ce type d\'objet n\'existe pas'
        elif (cmd[0]=='level' or cmd[0]=='lvl') and len(cmd)>1:
            try:
                if 0<int(cmd[1])<=200:
                    self.level=int(cmd[1])
                else:
                    print 'Valeur invalide'
            except ValueError:
                print 'Valeur invalide'
        else:
            print 'Commande invalide'
        return True

    def set(self,stat,val):
        self.stats[stat][1]=val

    def getVal(self,stat):
        return self.stats[stat][1]

    def load(self):
        pass

    def save(self):
        root = etree.Element('item')
        node = etree.Element('name')
        node.text=self.name
        root.append(node)
        node = etree.Element('category')
        node.text=unicode(self.category)
        root.append(node)
        node = etree.Element('level')
        node.text=unicode(self.level)
        root.append(node)
        node = etree.Element('stats')
        for stat in self.stats.keys():
            child = etree.Element('stat',{'type':stat})
            child.text=unicode(self.stats[stat][1])
            node.append(child)
        root.append(node)
        etree.dump(root)


test = Item()
test.save()
